package com.sgcc.ln.pmos.e_ln_personalcenter.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.ln.pmos.common.utils.BigDecimalUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_personalcenter.bean.KeyValueBean;

import java.math.BigDecimal;

public class SetPowerRatioAdapter extends BaseQuickAdapter<KeyValueBean, BaseViewHolder> {

    private Context mContext;
    private TextView mTextView;
    private String mFrom;

    public SetPowerRatioAdapter(Context context, String from, TextView textView) {
        super(R.layout.item_setpowerratio);
        mContext = context;
        mTextView = textView;
        mFrom = from;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, KeyValueBean bean) {
        baseViewHolder.setText(R.id.tvKey, bean.getKey());
        EditText editText = (EditText) baseViewHolder.getView(R.id.etValue);
        editText.setText(bean.getValue());


        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editText.addTextChangedListener(textWatcher);
                } else {
                    editText.removeTextChangedListener(textWatcher);

                }
            }

            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    String value = editable.toString().trim();
                    try {
                        if (!TextUtils.isEmpty(value)) {
                            if ("2".equals(mFrom)) {
                                mTextView.setText("——");
                                if (value.contains("-")) {
                                    if (value.length() > 1) {
                                        int compare = BigDecimalUtils.compareTo(new BigDecimal(value), new BigDecimal("-1"));
                                        // num>num1返回1,num<num1返回-1
                                        if (compare == -1) {
                                            editText.setText("-1");
                                            ToastUtils.showShort("偏差比例不能低于-1");
                                            editText.setSelection("-1".length());
                                        } else {
                                            bean.setValue(value);
                                        }
                                    }
                                } else {
                                    bean.setValue(value);
                                }
                            } else {
                                if (BigDecimalUtils.compareTo(new BigDecimal(value), BigDecimal.ONE) == 1) {
                                    editText.setText("");
                                    ToastUtils.showShort("单一时段用电量占比不能大于1");
                                } else {
                                    bean.setValue(value);
                                    BigDecimal result = BigDecimal.ZERO;
                                    for (KeyValueBean keyValueBean : getData()) {
                                        if (keyValueBean != null && !TextUtils.isEmpty(keyValueBean.getValue())) {
                                            result = BigDecimalUtils.add(result, new BigDecimal(keyValueBean.getValue()));
                                        }
                                    }
                                    if (BigDecimalUtils.compareTo(result, BigDecimal.ONE) == 1) {
                                        ToastUtils.showShort("各时段用电量占比之和不能大于1");
                                    }
                                    mTextView.setText(result.stripTrailingZeros().toPlainString());
                                }

                            }
                        }else {
                            bean.setValue("");
                        }
                    } catch (Exception e) {
                        ToastUtils.showShort("请输入正确格式的数字");
                        editText.setText("");
                    }
                }
            };
        });


    }

}
